package suncertify.ui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * The view to show the user when the server is started and running.
 * This view will be shown the entire time the server is running.
 * To stop the server, click the stop button.
 * @author Paul Wilhelm
 *
 */
public class ServerRunningGui {
	
	private JLabel descriptionLbl;
	private JButton stopBtn;
	private JFrame frame;
	private static ServerRunningGui INSTANCE;		//A reference to the object to be used with the singleton pattern
	private Container mainPanel;
	private GridBagConstraints constraint;
	
	//Private, not a requirement to document
	private ServerRunningGui(){
		constraint = new GridBagConstraints();
		frame = new JFrame("Server Status");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridBagLayout());
		frame.setPreferredSize(new Dimension(300,300));
		Image im = Toolkit.getDefaultToolkit().getImage("icon.gif");
		frame.setIconImage(im);
		mainPanel = frame.getContentPane();
	}
	
	/**
	 * Singleton. Returns the <code>ServerRunningGui</code> object already created in the JVM.  
	 * If no object exists, it creates one.
	 * @return The ServerRunningGui object in memory
	 */
	public static ServerRunningGui getInstance(){
		if (INSTANCE == null){						//If no object has been created
			INSTANCE = new ServerRunningGui();
		}
		return INSTANCE;
	}
	
	/**
	 *  Creates the GUI for the user to interact with.  It then shows the gui on the screen.
	 */
	public void buildAndShowGui(){
		constraint.gridx = 0;
		descriptionLbl = new JLabel("<HTML><h2>Server is Running</h2></HTML>");
		mainPanel.add(descriptionLbl, constraint);
		constraint.insets = new Insets(20, 20, 20, 20);
		stopBtn = new JButton("Stop");
		stopBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mainPanel.add(stopBtn, constraint);
		frame.pack();
		frame.setVisible(true);
	}

}
